package com.kashew;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class MainWindow extends JFrame implements Runnable {
	public static final String VERSION="0.5.0";
	private static final long serialVersionUID = -7080479181994091775L;
	Dimension windowSize = new Dimension(320, 480);
	static File trayIconFile = new File(System.getProperty("user.dir")+"/lib/tray.png");
	static TrayIcon trayIcon = new TrayIcon(new ImageIcon(trayIconFile.toString(), "Tray icon").getImage());
	static File iconFile = new File(System.getProperty("user.dir")+"/lib/chatty.png");
	static MainWindow window;
	static MenuItem showWindow = new MenuItem("Show Chatty");
	public MainWindow() {
		SystemTray tray = SystemTray.getSystemTray();
		Image icon = null;
		try {
			icon = ImageIO.read(iconFile);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			tray.add(trayIcon);
		} catch (AWTException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		MenuItem trayExit = new MenuItem("Exit");
		trayExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		showWindow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				window.setVisible(true);
				window.requestFocus();
			}
		});
		showWindow.setEnabled(false);
		PopupMenu trayMenu = new PopupMenu();
		trayMenu.add(showWindow);
		trayMenu.add(trayExit);
		trayIcon.setPopupMenu(trayMenu);
		trayIcon.setToolTip("Chatty - Version "+VERSION);
		setTitle("Chatty");
		setSize(windowSize);
		setIconImage(icon);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(HIDE_ON_CLOSE);
		init();
	}
	public static void main(String[] args) {
    	SwingUtilities.invokeLater(new Runnable() {
    		public void run() {
    			window = new MainWindow();
    			window.setVisible(true);
    			window.addWindowListener(new WindowListener() {
    				@Override
    	            public void windowClosing(WindowEvent arg0) {
    	            }
					@Override
					public void windowActivated(WindowEvent e) {
					}
					@Override
					public void windowClosed(WindowEvent e) {
					}
					@Override
					public void windowDeactivated(WindowEvent e) {
					}
					@Override
					public void windowDeiconified(WindowEvent e) {
					}
					@Override
					public void windowIconified(WindowEvent e) {
					}
					public void windowOpened(WindowEvent e) {
						showLogin();
					}
    	        });
    		}
    	});
	}
	public void init() {
		Thread t = new Thread(this);
		t.start();
	}
	public void run() {
		while(true) {
			if(!window.isVisible()) {
				showWindow.setEnabled(true);
			} else {
				showWindow.setEnabled(false);
			}
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static void showLogin() {
		JPasswordField pwField = new JPasswordField();
	    JTextField usrField = new JTextField();
	    Object[] options = {"Login", "Register", "Cancel"};
		Object[] inputs = new Object[] {
                new JLabel("Username:"),
                usrField,
				new JLabel("Password:"),
                pwField,
		};
		int result = JOptionPane.showOptionDialog(null, inputs, "Chatty", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[1]);
		if(result == JOptionPane.YES_OPTION) {
			if(usrField.getText().length() > 0 & pwField.getPassword().toString().length() > 0) {
				// TODO: Login
			} else {
				JOptionPane.showMessageDialog(null, "You must enter a username and password.", "Error!", JOptionPane.ERROR_MESSAGE);
				showLogin();
			}
		} else if(result == JOptionPane.NO_OPTION) {
			showRegister();
		} else if(result == JOptionPane.CANCEL_OPTION) {
			System.exit(0);
		}
	}
	public static void showRegister() {
		// TODO: Registration
	}
}
